﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using Excel = Microsoft.Office.Interop.Excel;

namespace Simulation
{
	class DataGrid
	{
		C_MSSG MG = new C_MSSG();

		private int		S = 0;
		private	int		E = 0;
		private int		J = 0;
		private object	DG;
		private object	FD;

		string FileName;
		string DefaultExt	= "xls";
		string Filter		= "Excel files (*.xls)|*.xls";
		string Directory	= "C:\\Users\\SMNAM\\Desktop";

		public DataGrid()
		{
			this.S = 0;
			this.E = 0;
			this.J = 0;
		}

		public void SetGrid( object sdg )
		{
			this.DG = sdg;
		}

		public void SetExcel( string name, int sj, object sdg, object sfd )
		{
			FileName	= name;
			this.J		= sj + 1;
			this.DG		= sdg;
			this.FD		= sfd;

		}

		public void Set( int ss, int se, int sj, object sdg )
		{
			this.S	= ss;
			this.E	= se;
			this.J	= sj;
			this.DG = sdg;
		}

		public void SetProperties()
		{
			for ( int i = S; i < E; i++ )
				( ( DataGridView )DG ).Columns[i].Visible = false;

		}

		public void SetFlagStar()
		{
			if ( ( ( ( DataGridView )DG ).CurrentCell.RowIndex == 0 ) && ( ( ( DataGridView )DG ).CurrentCell.ColumnIndex == 0 ) )
				return;

			if ( ( ( ( DataGridView )DG )[( ( DataGridView )DG ).CurrentCell.ColumnIndex, ( ( DataGridView )DG ).CurrentCell.RowIndex].Value ) != ( ( ( DataGridView )DG )[( ( DataGridView )DG ).CurrentCell.ColumnIndex + J + 1, ( ( DataGridView )DG ).CurrentCell.RowIndex].Value ) )
				( ( DataGridView )DG )[J, ( ( DataGridView )DG ).CurrentCell.RowIndex].Value = '*';
		}

		public void SetFlagAdd()
		{
			( ( DataGridView )DG ).Rows.Add( 1 );
			( ( DataGridView )DG ).CurrentCell = ( ( DataGridView )DG )[0, ( ( DataGridView )DG ).RowCount - 2];
			( ( DataGridView )DG )[J, ( ( DataGridView )DG ).RowCount-2].Value = '+';
		}

		public void SetFlagMinus()
		{
			if ( ( ( DataGridView )DG )[0, ( ( DataGridView )DG ).CurrentCell.RowIndex].Value == null )
				( ( DataGridView )DG ).Rows.Remove( ( ( DataGridView )DG ).Rows[( ( DataGridView )DG ).CurrentCell.RowIndex] );
			else
				( ( DataGridView )DG )[J, ( ( DataGridView )DG ).CurrentCell.RowIndex].Value = '-';
		}

		public string SetColor()
		{
			try 
			{
				switch ( ( ( DataGridView )DG )[J, ( ( DataGridView )DG ).CurrentCell.RowIndex].Value.ToString() )
				{
					case "*":
						( ( DataGridView )DG ).Rows[( ( DataGridView )DG ).CurrentCell.RowIndex].DefaultCellStyle.BackColor = Color.PaleTurquoise;
						break;
					case "+":
						( ( DataGridView )DG ).Rows[( ( DataGridView )DG ).CurrentCell.RowIndex].DefaultCellStyle.BackColor = Color.Gold;
						break;
					case "-":
						( ( DataGridView )DG ).Rows[( ( DataGridView )DG ).CurrentCell.RowIndex].DefaultCellStyle.BackColor = Color.Salmon;
						break;
				}
			}
			catch ( Exception e )
			{
				return string.Format( "{0}", e );
			}

			return "OK";
		}

		public void SetClear()
		{
			( ( DataGridView )DG ).Rows.Clear();
		}

		public string SetFlag( int C, int R )
		{
			string flag = string.Format( "{0}", ( ( DataGridView )DG )[C, R].Value );
		    return flag;
		}

		public void AlignCell( string CL, string F )
		{
			if ( F == "R" )
				( ( DataGridView )DG ).Columns[CL].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
			else if ( F == "C" )
				( ( DataGridView )DG ).Columns[CL].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
		}

		public int CheckNull( int C, int R )
		{
			try
			{
				string V = ( ( DataGridView )DG )[C, R].Value.ToString();
			
				if ( V.Trim().Length == 0 )
						return -1;
			}
			catch
			{
				return -1;
			}
			return 0;
		}

		public void SetFocus( int C, int R )
		{
			( ( DataGridView )DG ).CurrentCell =  ( ( DataGridView )DG )[C, R];
		}

		public void PutBlink( int C, int R )
		{
			string V = string.Format( "{0}", ( ( DataGridView )DG )[C, R].Value );

			if ( V.Trim().Length == 0 )
				( ( DataGridView )DG )[C, R].Value = " ";
		}

		public void ExportExcel( bool captions )
		{
			( ( SaveFileDialog )FD).FileName			= FileName;
			( ( SaveFileDialog )FD ).DefaultExt			= DefaultExt;
			( ( SaveFileDialog )FD ).Filter				= Filter;
			( ( SaveFileDialog )FD ).InitialDirectory	= Directory;

			DialogResult result = ( ( SaveFileDialog )FD ).ShowDialog();

			if ( result == DialogResult.OK )
			{
				int num = 0;
				object missingType = Type.Missing;

				Excel.Application objApp;
				Excel._Workbook objBook;
				Excel.Workbooks objBooks;
				Excel.Sheets objSheets;
				Excel._Worksheet objSheet;
				Excel.Range range;

				string[] headers = new string[( ( DataGridView )DG ).ColumnCount - J];
				string[] columns = new string[( ( DataGridView )DG ).ColumnCount - J];

				for ( int c = 0; c < ( ( DataGridView )DG ).ColumnCount - J; c++ )
				{
					headers[c] = ( ( DataGridView )DG ).Rows[0].Cells[c].OwningColumn.HeaderText.ToString();
					num = c + 65;
					columns[c] = Convert.ToString( ( char )num );
				}

				try
				{
					objApp = new Excel.Application();
					objBooks = objApp.Workbooks;
					objBook = objBooks.Add( Missing.Value );
					objSheets = objBook.Worksheets;
					objSheet = ( Excel._Worksheet )objSheets.get_Item( 1 );

					if ( captions )
					{
						for ( int c = 0; c < ( ( DataGridView )DG ).ColumnCount - J; c++ )
						{
							range = objSheet.get_Range( columns[c] + "1", Missing.Value );
							range.set_Value( Missing.Value, headers[c] );
						}
					}

					for ( int r = 0; r < ( ( DataGridView )DG ).RowCount - 1; r++ )
					{
						for ( int c = 0; c < ( ( DataGridView )DG ).ColumnCount - J; c++ )
						{
							if ( ( ( DataGridView )DG ).Rows[r].Cells[c].Value == null )
								continue;

							range = objSheet.get_Range( columns[c] + Convert.ToString( r + 2 ), Missing.Value );
							range.set_Value( Missing.Value, ( ( DataGridView )DG ).Rows[r].Cells[c].Value.ToString() );
						}
					}

					objApp.Visible = false;
					objApp.UserControl = false;

					objBook.SaveAs( ( ( SaveFileDialog )FD ).FileName,
							  Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,
							  missingType, missingType, missingType, missingType,
							  Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
							  missingType, missingType, missingType, missingType, missingType );
					objBook.Close( false, missingType, missingType );

					Cursor.Current = Cursors.Default;

					MessageBox.Show( MG.COMM_SAVE_SUCC );
				}
				catch ( Exception theException )
				{
					String errorMessage;
					errorMessage = "Error: ";
					errorMessage = String.Concat( errorMessage, theException.Message );
					errorMessage = String.Concat( errorMessage, " Line: " );
					errorMessage = String.Concat( errorMessage, theException.Source );

					MessageBox.Show( errorMessage, MG.COMM_SAVE_ERRR );
				}
			}
		}
	}
}
